package com.beyond.pm.dp

import android.content.Context
import androidx.compose.ui.graphics.Color
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import com.beyond.common.ext.dataStore
import com.beyond.common.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object LanguagePreference {
    private const val LANGUAGE_NAME = "LANGUAGE_NAME"

    val values: List<LocalizedStrings> = mutableListOf(
        LocalizedStrings(
            type = "zh",
            switchLanguageButton = "切换语言"
        ),
        LocalizedStrings(
            type = "en",
            switchLanguageButton = "Switch Language"
        )
    )

    val default = values[1].type

    private val key = stringPreferencesKey(LANGUAGE_NAME)

    fun put(context: Context, scope: CoroutineScope, value: String) {
        scope.launch(Dispatchers.IO) {
            context.dataStore.put(key, value)
        }
    }

    fun fromPreferences(preferences: Preferences) = preferences[key] ?: default

    data class LocalizedStrings(
        val type: String,
        val switchLanguageButton: String
    )
}
